package Listener.lab3;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @Description:
 * @Author:AMark
 * @Date: 10/30/2023
 */
public class ButtonExchangeDemo {
    public JButton buttonok,buttoncancel;
    public JFrame Buttonframe;
    ButtonExchangeDemo(String title) {
        Buttonframe = new JFrame(title);
        Buttonframe.setSize(260, 150);
        Buttonframe.setLayout(new FlowLayout());

        buttoncancel = new JButton("取消");
        buttonok = new JButton("确定");
        buttoncancel.setSize(20, 20);
        buttonok.setSize(20, 20);


        buttonok.addActionListener(new ActionListener() {
            String temp = new String();
            @Override
            public void actionPerformed(ActionEvent e) {
                temp = buttoncancel.getText();
                buttoncancel.setText(buttonok.getText());
                buttonok.setText(temp);
            }
        });
        buttoncancel.addActionListener(new ActionListener() {
            String temp = new String();
            @Override
            public void actionPerformed(ActionEvent e) {
                temp = buttonok.getText();
                buttonok.setText(buttoncancel.getText());
                buttoncancel.setText(temp);
            }
        });

        Buttonframe.add(buttoncancel);
        Buttonframe.add(buttonok);
        Buttonframe.setVisible(true);
        Buttonframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
        new ButtonExchangeDemo("按钮内容交换");
    }
}
